package dataStruct;

/**
 * @Author wangtengyu
 * @Create 2018-03-22-16:04
 */
public class MyQueue {

    //存储队列数据
    private Object queArray[];

    //队尾
    private int rear;

    //队头
    private int front;

    //队列中元素个数
    private int maxSize;

    //队列中实际元素个数
    private int items;

    public MyQueue(int maxSize){
        this.maxSize=maxSize;
        queArray=new Object[maxSize];
        this.front=0;
        this.rear=-1;
        this.items=0;
    }


    //插入数据
    public void insert(int value){
        if (isFull()){
            System.out.println("队列已满");
        }
        if(rear==maxSize-1){
            rear=-1;
        }
        queArray[++rear]=value;
        items++;
    }

    //删除数据
    public Object remove(){
        Object removeObj=null;
        if(!isEmpty()){
            removeObj=queArray[front];
            queArray[front]=null;
            front++;
            if(front==maxSize){
                front=0;
            }
            items--;
            return removeObj;
        }
        return removeObj;
    }

    //获得队头数据
    public Object peekFront(){
        return queArray[front];
    }

    //查看队列是否为空
    public boolean isEmpty(){
        return (this.items==0);
    }

    //查看队列是否满了
    public boolean isFull(){
        return (this.items==this.maxSize);
    }


    //返回队列的大小
    public int getSize(){
        return this.items;
    }


    public static void main(String[] args) {
        MyQueue queue = new MyQueue(3);
        queue.insert(1);
        queue.insert(2);
        queue.insert(3);//queArray数组数据为[1,2,3]

        System.out.println(queue.peekFront()); //1
        queue.remove();//queArray数组数据为[null,2,3]
        System.out.println(queue.peekFront()); //2

        queue.insert(4);//queArray数组数据为[4,2,3]
        queue.remove();
        System.out.println(queue.peekFront());
        queue.insert(5);//队列已满,queArray数组数据为[4,2,3]
    }





}
